package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增套餐
     */
    @Override
    @Transactional
    public void addSetcmeal(SetmealDTO setmealDTO) {
        //将请求参数数据装到数据库数据中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.addSetcmeal(setmeal);
        //遍历
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 分页查询
     */
    @Override
    @Transactional
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPage) {
        PageHelper.startPage(setmealPage.getPage(),setmealPage.getPageSize());
        Page<PageResult> pg = setmealMapper.pageSetmeal(setmealPage);
        PageResult page=new PageResult(pg.getTotal(),pg.getResult());
        return page;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void setmealDeletes(List<Long> ids) {
        if (ids != null && ids.size() > 0) {
            int count = setmealMapper.getCountByStatusAndId(StatusConstant.ENABLE, ids);
            if (count > 0) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealMapper.setmealDeletes(ids);
            for (Long setmeaId : ids) {
                setmealDishMapper.deleteBySetmealId(setmeaId);
            }
        }
    }

    /**
     * 套餐起售,停售
     * @param status
     * @param setmealId
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, Long setmealId) {
        if (status.equals(StatusConstant.ENABLE)) {
            // 查询套餐对应的菜品是否包含停售，如果包含则启动失败
            int count = setmealDishMapper.getDishEnableCountByStatusAndSetmealId(StatusConstant.DISABLE, setmealId);
            if (count > 0) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(setmealId);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    @Transactional
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        // 修改套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateById(setmeal);
        // 修改套餐关联菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            // 删除关联菜品信息
            setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
            // 添加新的关联菜品
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
